//引入
let http = require("http");
let fs = require("fs");
let path = require("path");

//创建服务器
let server = http.createServer();

server.on("request", function (req, resp) {
    console.log(req.url);
    // req.on('end')
    // console.log(decodeURIComponent(req.url));
    // console.log(req.method);
    resp.setHeader("Content-Type", "text/html; charset=utf-8");
    let htmlPath;//访问的html页面路径
    if (req.url == "/" || req.url.endsWith("index.html")) {
        console.log("访问index.html");
        htmlPath = path.join(__dirname, "index.html");
    } else if (req.url.endsWith("demo.html")) {
        console.log("访问login.html");
        htmlPath = path.join(__dirname, "demo.html");
    }

    if (req.url != "/favicon.ico" && htmlPath) {
        fs.readFile(htmlPath, "utf-8", function (err, data) {
            if (!err) {
                resp.end(data);
            } else {
                resp.end(err);
            }
        });
    } else {
        resp.end("404：" + htmlPath);
    }

});

server.listen(8080, function () {
    console.log("服务器启动成功");
});